#include <iostream>
#include "stdfreader.h"
#include "stdfconfig.h"
#include "cmdlineopts.h"
#include "debugprintdetail.h"
#include "fileprintdetail.h"

using namespace std;
using namespace opsrc::stdf;
using namespace opsrc;


int main(int argc, char** argv) {


    // ***** StdfReader flag and option settings *********
    StdfConfig* cfg = StdfConfig::instance();

    if( argc >1 )
    {
        cmdlineopts cl;  // command line decoder

        if (cl.parse(argc,argv)) {

            if( cl.has_configfile() )
            {
                cout << "reading config file "  << cl.config_filename() << endl;
                cfg->setConfigFile(cl.config_filename()); // read all cfg file parameter settings
            }
            else
            {
                cout << "error parsing passing arguments" << endl;
                return -1;
            }
        } else {
            cout << "error command line decoder" << endl;
        }

        if( cl.has_stdffile())
            cfg->setFilename( cl.stdf_filename() );

        if (cl.has_stdffields()) {
            cfg->stdfFieldsRecSettingInitializeAs(false);
            vector<string> svec = cl.stdffields();
            for( vector<string>::iterator it = svec.begin(); it != svec.end(); ++it){
                cfg->updateSetting(*it, true);
             }
        }
        else {
            cfg->stdfFieldsRecSettingInitializeAs(true);
        }

        cfg->updateSetting("DEBUG", cl.has_debug());

        // set test numbers if passed in from command line
        if (cl.has_testnumbers()){
            cfg->setTestNumbers(cl.testnumbers());
         }
    }
    else
    {
        cfg->setConfigFile("StdfParser.cfg"); // read all cfg file parameter settings
    }

    OutFormat *of;
    if( cfg->getIsDebugSwitchSet() || cfg->getOutputMode() == "D_PRINT") {
        of = new DebugPrintDetail();
        cout << "debug mode" << endl;
    }
    else {
        of = new FilePrintDetail();
    }

    
    StdfReader app;  // create App object
    app.setOutFormat(of);
    app.initialize();

    // process STDF file
    app.read();
    return 0;
}
